<template>
  <div class="account-data">
    <div class="add-box">
      <el-button @click="addAccount" type="primary">新增摄影师信息</el-button>
    </div>
    <!-- 数据列表 -->
    <el-table :data="tableData" border style="width:100%" v-loading="loading" element-loading-text="拼命加载中">
      <el-table-column label="等级" width="180">
        <template slot-scope="scope">
          <span>{{ roleName(scope.row.Sgrade) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="姓名" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.Sname}}</span>
        </template>
      </el-table-column>

      <el-table-column label="年龄">
        <template slot-scope="scope">
          <span>{{scope.row.Sage}}</span>
        </template>
      </el-table-column>

      <el-table-column label="从业时间">
        <template slot-scope="scope">
          <span>{{scope.row.Sjobtime}}</span>
        </template>
      </el-table-column>

      <el-table-column label="价格">
        <template slot-scope="scope">
          <span>{{scope.row.Smoney}}</span>
        </template>
      </el-table-column>

      <el-table-column label="联系方式">
        <template slot-scope="scope">
          <span>{{scope.row.Snumber}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row)" size="mini" type="danger">删除</el-button>
          <el-button @click="handleEdit(scope.row)" size="mini">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pages" ref="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        layout="prev, pager, next"
        :page-size="pages"
        :total="total">
      </el-pagination>
    </div>

    <!-- 新增 -->
    <el-dialog
      title="新增"
      :visible.sync="dialogVisibleAdd"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form :rules="SgradeFrom" ref="AddForm" :model="listObj" class="form-box" >
        <el-form-item label="请选择等级" prop="Sgrade">
          <el-select @change="change" v-model="listObj.Sgrade" placeholder="请选择等级">
            <el-option
              v-for="option in options"
              :label="option.text"
              :value="option.key"
              :key="option.key"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="请输入姓名" prop="Sname">
          <el-input v-model="listObj.Sname" placeholder="请输入姓名"></el-input>
        </el-form-item>

        <el-form-item label="请输入年龄" prop="Sage">
          <el-input v-model="listObj.Sage" placeholder="请输入年龄"></el-input>
        </el-form-item>

        <el-form-item label="请输入从业时间" prop="Sjobtime">
          <el-input v-model="listObj.Sjobtime" placeholder="请输入从业时间"></el-input>
        </el-form-item>

        <el-form-item label="请输入价格" prop="Smoney">
          <el-input v-model="listObj.Smoney" placeholder="请输入价格"></el-input>
        </el-form-item>

        <el-form-item label="请输入联系方式" prop="Snumber">
          <el-input v-model="listObj.Snumber" placeholder="请输入账号"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button @click="handleAdd" type="primary" :loading="loading">确定</el-button>
      </span>
    </el-dialog>


    <!-- 编辑 -->
    <el-dialog
      title="编辑"
      :visible.sync="dialogVisibleEdit"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form :rules="SgradeFrom" ref="EditForm" :model="listObj" class="form-box" >
        <el-form-item label="请选择等级" prop="Sgrade">
          <el-select @change="change" v-model="listObj.Sgrade" placeholder="请选择等级">
            <el-option
              v-for="option in options"
              :label="option.text"
              :value="option.key"
              :key="option.key"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="请输入姓名" prop="Sname">
          <el-input v-model="listObj.Sname" placeholder="请输入姓名"></el-input>
        </el-form-item>

        <el-form-item label="请输入年龄" prop="Sage">
          <el-input v-model="listObj.Sage" placeholder="请输入年龄"></el-input>
        </el-form-item>

        <el-form-item label="请输入从业时间" prop="Sjobtime">
          <el-input v-model="listObj.Sjobtime" placeholder="请输入从业时间"></el-input>
        </el-form-item>

        <el-form-item label="请输入价格" prop="Smoney">
          <el-input v-model="listObj.Smoney" placeholder="请输入价格"></el-input>
        </el-form-item>

        <el-form-item label="请输入联系方式" prop="Snumber">
          <el-input v-model="listObj.Snumber" placeholder="请输入账号"></el-input>
        </el-form-item>
      </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button @click="handleEditSave()" type="primary" :loading="loading">确定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import jwt_decode from 'jwt-decode';

@Component({
  components: {  }
})
export default class AccountData extends Vue {
  getUser: any;
  tableData: any = [];
  total:number = 0; // 总数据量
  pages:number = 3; // 每页数据量
  pageNumber: number = 0; // 页码
  loading:boolean = true;// loading加载
  dialogVisibleAdd: Boolean = false; // 新增模态框显示状态
  dialogVisibleEdit: Boolean = false; // 编辑模态框显示状态
  Sheyinshiid:Number = 0; // 数据ID

  // 对象数据
  listObj: any = {
    Sgrade:'', Sname:'', Sage:'', Sjobtime:'', Smoney:'', Snumber:''
  };

  SgradeFrom: any = {
    Sgrade: [{ required: true, message: "请选择角色", trigger: "change" }],
    Sname: [{ required: true, message: "请输入姓名", trigger: "blur" }],
    Sage: [{ required: true, message: "请输入年龄", trigger: "blur" }],
    Sjobtime: [{ required: true, message: "请输入从业时间", trigger: "blur" }],
    Smoney: [{ required: true, message: "请输入价格", trigger: "blur" }],
    Snumber: [{ required: true, message: "请输入联系方式", trigger: "blur" }],
  };

  // select数据
  options: any = [
    {
      key: '1',
      text: "初级",
    },
    {
      key: '2',
      text: "中级",
    },
    {
      key: '3',
      text: "高级",
    }
  ];

  created() {
    this.getUser = jwt_decode(localStorage.tsToken);
    this.getData();
  }

  // 新增账户
  handleAdd() {
    (this as any).$refs["AddForm"].validate((valid: boolean) => {
      if (valid) {
        this.loading = true;
        const param:any = this.listObj;

        (this as any).$axios.post("/adminSheyinshi/add", param).then((res: any) => {
          if(res.data.code === 200){
            this.getData();
            this.closeDialog();
            this.loading = false;
            (this as any).$message({
              message: '添加成功',
              type: "success"
            })
          }
          else{
            (this as any).$message.error(res.data.data)
          }
        })
      }
    });
  }

  // 下拉框事件 - 选择角色
  change(e:any){
    this.listObj.Sgrade = e;
  }

  // 显示新增账户模态框
  addAccount() {
    this.dialogVisibleAdd = true;
  }

  // 隐藏新增账户
  closeDialog() {
    this.dialogVisibleAdd = false;
    this.dialogVisibleEdit = false;
  }

  // 删除
  handleDelete(row: any): void {
    (this as any).$confirm('是否确认删除？')
    .then((res:any)=> {
      const { Sheyinshiid } = row;

      (this as any).$axios.post('/adminSheyinshi/del', { Sheyinshiid }).then((res: any) => {
        if(res.data.code === 200){
            this.getData();
            (this as any).$message({
              message: res.data.data,
              type: "success"
            })
          }
          else{
            (this as any).$message.error(res.data.data)
          }
      })
    }) 
  }

  handleEdit(row: any){
    this.dialogVisibleEdit = true;
    const { Sheyinshiid, Sgrade, Sname, Sage, Sjobtime, Smoney, Snumber} = row;
    
    this.listObj.Sgrade = Sgrade;
    this.listObj.Sname = Sname;
    this.listObj.Sage = Sage;
    this.listObj.Sjobtime = Sjobtime;
    this.listObj.Smoney = Smoney;
    this.listObj.Snumber = Snumber;
    this.Sheyinshiid = Sheyinshiid;
  }
  // 编辑保存
  handleEditSave(){
    (this as any).$refs["EditForm"].validate((valid: boolean) => {
      if (valid) {
        this.loading = true;
        const param:any = this.listObj;
        param.Sheyinshiid = this.Sheyinshiid;

        (this as any).$axios.post("/adminSheyinshi/update", param).then((res: any) => {
          if(res.data.code === 200){
            this.getData();
            this.closeDialog();
            this.loading = false;
            (this as any).$message({
              message: '修改成功',
              type: "success"
            })
          }
          else{
            (this as any).$message.error(res.data.data)
          }
        })
      }
    });
  }

  // 数据显示
  getData() {
    this.loading = true;
    const param = {
      pages: this.pages,
      pageNumber: this.pageNumber
    };

    (this as any).$axios("/adminSheyinshi/sheyinshiList", { params:param }).then((res: any) => {
      this.loading = false;

      if(res.data.code === 200){
        this.tableData = res.data.data.list;
        this.total = res.data.data.sum;
      }
    })
  }

  // 分页 pageSize 改变时会触发
  handleSizeChange(val: number) {
    if(val === 1){
      this.pageNumber = 0
    }
    else{
      this.pageNumber = (val-1) * this.pages;
    }
    this.getData();
  }

  // 分页 currentPage 改变时会触发
  handleCurrentChange(val: number) {
    if(val === 1){
      this.pageNumber = 0
    }
    else{
      this.pageNumber = (val-1) * this.pages;
    }
    this.getData();
  }

  // 等级名称
  roleName(type:string){
    let str = ''

    switch(type){
      case '1': str = '初级'; break;
      case '2': str = '中级'; break;
      case '3': str = '高级'; break;
    }
    return str;
  }
}
</script>

<style lang="scss" scoped>
.account-data {
  height: 100%;
  overflow: auto;
  .add-box {
    margin-bottom: 10px;
  }
}
.pages{
  width: 100%;
  margin-top: 10px;
  background: white;
  display: flex;
  justify-content: flex-end;
  padding: 20px 0;
}

.form-box {
  .el-input,
  .el-select {
    width: 400px !important;
  }
}
</style>
